import 'dart:convert';

import 'package:eplorex/providers/user_context.dart';
import 'package:oktoast/oktoast.dart';

import '../models/group/group_base_info_model.dart';
import '../models/group/group_member_model.dart';
import '../utils/HttpUtil.dart';

class GroupDao {
  /// 创建群，返回群id
  static Future<Map<String, dynamic>> createGroup(
      String groupName, String groupDesc, String avatar, bool visible) async {
    var userId = UserContext.cxt()!.uid;
    var response =
        await HttpUtil.getInstance().post('/api/chat/v1/group/create',
            data: jsonEncode({
              "creatorId": userId,
              "groupName": groupName,
              "groupDesc": groupDesc,
              "avatar": avatar,
              "visible": visible
            }));

    if (response!.statusCode == 200 && response.data['code'] == 0) {
      return response.data['data'];
    }
    return {};
  }

  static Future joinGroup(num groupId, int source) async {
    var response = await HttpUtil.getInstance().post('/api/chat/v1/group/join',
        data: jsonEncode({
          'userId': UserContext.cxt()!.uid,
          'groupId': groupId,
          'source': source
        }));

    if (response!.statusCode == 200 && response.data['code'] != 0) {
      showToast('${response.data['msg']}');
      return;
    }
    showToast('失败');
  }

  static Future leaveGroup(num groupId) async {
    var userId = UserContext.cxt()!.uid;
    var response = await HttpUtil.getInstance().post('/api/chat/v1/group/leave',
        data: jsonEncode({"userId": userId, "groupId": groupId}));

    if (response!.statusCode == 200 && response.data['code'] != 0) {
      showToast('${response.data['msg']}');
      return;
    }
    showToast('失败');
  }

  static Future dismissGroup(num groupId) async {
    var userId = UserContext.cxt()!.uid;
    var response = await HttpUtil.getInstance().post(
        '/api/chat/v1/group/dismiss',
        data: jsonEncode({"userId": userId, "groupId": groupId}));

    if (response!.statusCode == 200 && response.data['code'] != 0) {
      showToast('${response.data['msg']}');
      return;
    }
    showToast('失败');
  }

  static Future<GroupBaseInfoModel?> queryGroupBaseInfo(num groupId) async {
    var userId = UserContext.cxt()!.uid;
    var response = await HttpUtil.getInstance().get(
        '/api/chat/v1/group/base/info',
        data: {"userId": userId, "groupId": groupId});

    if (response!.statusCode == 200 && response.data['code'] == 0) {
      return GroupBaseInfoModel.fromJson(response.data['data']);
    }
    return null;
  }

  static Future<Iterable<GroupBaseInfoModel>?> queryRecommendGroups() async {
    var response = await HttpUtil.getInstance().get(
        '/api/chat/v1/group/recommend');
    if (response!.statusCode == 200 && response.data['code'] == 0) {
      return (response.data['data'] as List).map((e) => GroupBaseInfoModel.fromJson(e)) ;
    }
    return [];
  }

  static Future<List<GroupMemberModel>?> queryGroupMember(num groupId) async {
    var userId = UserContext.cxt()!.uid;
    try {
      var response = await HttpUtil.getInstance().get(
          '/api/chat/v1/group/members',
          data: {"userId": userId, "groupId": groupId});
      if (response!.statusCode == 200 && response.data['code'] == 0) {
        return (response.data['data'] as List)
            .map((e) => GroupMemberModel.fromJson(e))
            .toList();
      }
    } catch (e) {
      print(e);
    }
    return [];
  }


}
